home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / Codeworks 0.94b3 / Codeworks® WWW Demo Doc. / Scripting Manual.doc / Scripting Manual.doc.rsrc / TEXT_141.txt < prev    next >
Encoding:
Text File  |  1995-05-01  |  1.4 KB  |  20 lines

  1. Writing Scripts - Defaulted Arguments
  2.  
  3.     In the previous example, it might be reasonable to assume that most of the time, the quantity argument is going to be 1.  It would be convenient if messages that had a quantity of 1 didn‚Äôt even have to write it.  For example:
  4.  
  5.         ring-up-item cash-register price 17.29.
  6.  
  7.     If you run the expression above, a debugger will appear with the error message ‚ÄúUsing an undefined value‚Äù and q selected.  This is because the arguments to the message didn‚Äôt give a value to q, so its value is undefined.  This is the same error as using a local variable before you‚Äôve assigned it a value.
  8.  
  9.     Scripts can handle missing values rather than just causing an error:  They can default the value of the argument.  In essence, we want assign 1 to q if the message didn‚Äôt give it a value in the first place.  There is a form of assignment, ?=, just for this case.  It is called defaulting.  Change the ring-up-item script to:
  10.  
  11.          $ price p, quantity q.
  12.         $ cost.
  13.         q ?= 1.
  14.         cost := p * q.
  15.         sub-total := sub-total + cost.
  16.     
  17.     Now, if argument quantity is part of the message, then q will be set by the message, otherwise it will be set to 1.
  18.  
  19. ¬ª    This is actually the only way that you can assign to the variables for arguments.  Argument variables, like p and q, cannot be assigned to using :=.  Remember: argument variables are assigned by the message, local variables are assigned by your script.
  20.